package com.hgyc.mom.qms.controller;

import java.util.*;
import jakarta.servlet.http.HttpServletResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.BeanUtils;
import org.springframework.web.multipart.MultipartFile;
import com.hgyc.mom.qms.entity.QmsCollectRecord;
import com.hgyc.mom.qms.vo.QmsCollectRecordParam;
import com.hgyc.mom.qms.service.QmsCollectRecordService;
import com.hgyc.mom.common.vo.Result;
import com.hgyc.mom.common.util.poi.ExcelUtil;

import com.hgyc.mom.core.mybatis.QueryWrapperUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

/**
 * 不良信息采集记录Controller
 *
 * @author fwj
 * @since 2025-07-30
 */
@Tag(name = "不良信息采集记录控制器", description = "不良信息采集记录控制器")
@RestController
@RequestMapping("/qms/collect/record")
public class QmsCollectRecordController {
    @Autowired
    private QmsCollectRecordService qmsCollectRecordService;

    @Operation(summary = "查询不良信息采集记录列表", description = "查询不良信息采集记录列表")
    @PostMapping("/pageList")
    public Result<IPage<QmsCollectRecord>> pageList(@Parameter(description = "查询参数") @RequestBody QmsCollectRecordParam qmsCollectRecordParam) {
        Page<QmsCollectRecord> page = new Page<>(qmsCollectRecordParam.getPage(), qmsCollectRecordParam.getPageSize());
        QmsCollectRecord qmsCollectRecord = new QmsCollectRecord();
        BeanUtils.copyProperties(qmsCollectRecordParam, qmsCollectRecord);
        QueryWrapper<QmsCollectRecord> wrapper = QueryWrapperUtil.build(qmsCollectRecord);
        IPage<QmsCollectRecord> list = qmsCollectRecordService.page(page, wrapper);
        return Result.success(list);
    }


    @Operation(summary = "获取不良信息采集记录详细信息", description = "获取不良信息采集记录详细信息")
    @GetMapping(value = "/{id}")
    public Result getInfo(@Parameter(description = "不良信息采集记录ID") @PathVariable("id") Long id) {
        return Result.success(qmsCollectRecordService.getById(id));
    }
    @Operation(summary = "新增不良信息采集记录", description = "新增不良信息采集记录")
    @PostMapping
    public Result<?> save(@Parameter(description = "不良信息采集记录") QmsCollectRecord qmsCollectRecord) {
        qmsCollectRecordService.saveRecord(qmsCollectRecord);
        return Result.success();
    }

    @Operation(summary = "修改不良信息采集记录", description = "修改不良信息采集记录")
    @PutMapping
    public Result<?> update(@Parameter(description = "不良信息采集记录") @RequestBody QmsCollectRecord qmsCollectRecord) {
        qmsCollectRecordService.updateRecord(qmsCollectRecord);
        return Result.success();
    }

    @Operation(summary = "删除不良信息采集记录", description = "删除不良信息采集记录")
    @DeleteMapping
    public Result<?> remove(@Parameter(description = "删除ID") @RequestParam("ids") Long[] ids) {
        return Result.success(qmsCollectRecordService.removeByIds(Arrays.asList(ids)));
    }

    @Operation(summary = "下载导入模版", description = "下载导入模版")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<QmsCollectRecord> util = new ExcelUtil<>(QmsCollectRecord.class);
        util.importTemplateExcel(response, "不良信息采集记录");
    }

    @Operation(summary = "导出不良信息采集记录列表", description = "导出不良信息采集记录列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response, QmsCollectRecordParam qmsCollectRecordParam) {
        QmsCollectRecord qmsCollectRecord = new QmsCollectRecord();
        BeanUtils.copyProperties(qmsCollectRecordParam, qmsCollectRecord);
        QueryWrapper<QmsCollectRecord> wrapper = QueryWrapperUtil.build(qmsCollectRecord);
        List<QmsCollectRecord> list = qmsCollectRecordService.list(wrapper);
        ExcelUtil<QmsCollectRecord> util = new ExcelUtil<>(QmsCollectRecord.class);
        util.exportExcel(response, list, "不良信息采集记录数据");
    }

    @Operation(summary = "导入不良信息采集记录列表", description = "导入不良信息采集记录列表")
    @PostMapping("/import")
    public Result<?> importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<QmsCollectRecord> util = new ExcelUtil<>(QmsCollectRecord.class);
        List<QmsCollectRecord> qmsCollectRecordList = util.importExcel(file.getInputStream());
        String message = qmsCollectRecordService.importData(qmsCollectRecordList, updateSupport);
        return Result.success(message);
    }
}
